$(function () {
    //0 复选框全选事件
    $("#addNewStudentInformationShowFather").click(function () {
        var isChecked = $(this).prop("checked")
        if (isChecked) {
            $(".addNewStudentInformationShowSon").prop("checked", true);
        } else {
            $(".addNewStudentInformationShowSon").prop("checked", false);
        }
    });
    //1 学生登记界面 获取所有批次信息
    $.ajax({
        url: "/hospital/phyStudent/all.action",
        type: "GET",
        dataType: "json",
        success: function (data) {
            var $list = $("#tb_addNewStudent");
            $.each(data, function (index, ele) {
                var $item = createDataItem(index, ele);
                $list.append($item);
            });
        },
        error: function () {
            alert("获取数据失败");
        }
    });

    //1.1 动态创建一条数据
    function createDataItem(index, data) {
        var $item = $("" + "<tr>\n" +
            "            <td style=\"width:30px;\"><input class=\"addNewStudentInformationShowSon\" type=\"checkbox\"></td>\n" +
            "            <td>" + data.phyExmBatchName + "</td>\n" +
            "            <td>" + data.stuName + "</td>\n" +
            "            <td>" + data.phyExmBatchBeginTime + "</td>\n" +
            "            <td>" + data.phyExmBatchOverTime + "</td>\n" +
            "            <td>" + data.proName + "</td>\n" +
            // "            <td>\n" +
            // "                <button class=\"addNewStudentInformationShowUpdateBtn\" data-id=\"" + data.id + "\">编辑</button>\n" +
            // "                <button class=\"addNewStudentInformationShowCancelBtn\" data-id=\"" + data.id + "\">删除</button>\n" +
            // "            </td>\n" +
            "        </tr>");
        return $item;
    }

    //2 添加信息按钮监听事件
    $("#addNewStudentBtn").click(function () {
        $(".addNewStudentInfo").css("display", "block");
        $(".addNewStudentInformationShow").css("display", "none");
        //2.1 获取所有批次信息
        $("#addNewStudentInfoBatchSelect").html("");
        $("#addNewStudentInfoBatchSelect").append("<option value=''>请选择体检批次</option>");
        $.ajax({
            url: "/hospital/batch/all.action",
            type: "GET",
            dataType: "json",
            success: function (data) {
                var $batchlist = $("#addNewStudentInfoBatchSelect");
                $.each(data, function (index, ele) {
                    var $item = $("" + "<option value = '" + ele.id + "'>" + ele.PhyExmBatchName + "</option>");
                    $batchlist.append($item);
                });
            },
            error: function () {
                alert("获取批次失败");
            }
        });
        //2.1.1 获取所有学院信息
        $("#addNewStudentInfoDepramentSelect").html("");
        $("#addNewStudentInfoDepramentSelect").append("<option value=''>请选择体检学院</option>");
        $.ajax({
            url: "/hospital/department/all.action",
            type: "GET",
            dataType: "json",
            success: function (data) {
                var $batchlist = $("#addNewStudentInfoDepramentSelect");
                $.each(data, function (index, ele) {
                    var $item = $("" + "<option value = '" + ele.id + "'>" + ele.departName + "</option>");
                    $batchlist.append($item);
                });
            },
            error: function () {
                alert("获取学院失败");
            }
        });
    });
    //2.2 添加信息界面确认按钮监听************************************************************************************
    $("#addNewStudentInfoSureBtn").click(function () {
        //获取选择的批次和学院
        var batchId = $("#addNewStudentInfoBatchSelect option:selected").val();
        var deptId = $("#addNewStudentInfoDepramentSelect option:selected").val();
        $.ajax({
            url: "/hospital/phyStudent/add.action",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            data: JSON.stringify({
                "batchId": batchId,
                "deptId": deptId
            }),
            success: function (data) {
                //查询成功，返回成功数据
                alert("添加成功");
                var $list = $("#tb_addNewStudent");
                //1.清空表格数据
                $("#tb_addNewStudent tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item = createDataItem(index, ele);
                    $list.append($item);
                });
                alert("添加成功");
                $(".addNewStudentInfo").css("display", "none");
                $(".addNewStudentInformationShow").css("display", "block");
            },
            error: function () {
                alert("添加学生失败");
            }
        });
    });
    //2.3 添加信息界面取消按钮监听
    $("#addNewStudentInfoCancelBtn").click(function () {
        $(".addNewStudentInfo").css("display", "none");
        $(".addNewStudentInformationShow").css("display", "block");
    });

    //3 查询按钮监听事件
    //3.1 通过学生查询
    $("#selectByDepartmentBtn").click(function () {
        var stuName = $("#selectByDepartment").val();
        $.ajax({
            url: "/hospital/phyStudent/stuName.action",
            type: "POST",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({"stuName": stuName}),
            success: function (data) {
                //查询成功，返回成功数据
                var $list = $("#tb_addNewStudent");
                //1.清空表格数据
                $("#tb_addNewStudent tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item = createDataItem(index, ele);
                    $list.append($item);
                });
            },
            error: function () {
                alert("查询失败");
            }
        });
    });
    //3.2 通过批次查询
    $("#selectByBatchBtn").click(function () {
        var batchName = $("#selectByBatch").val();
        $.ajax({
            url: "/hospital/phyStudent/batch.action",
            type: "POST",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({"phyExmBatchName": batchName}),
            success: function (data) {
                //查询成功，返回成功数据
                var $list = $("#tb_addNewStudent");
                //1.清空表格数据
                $("#tb_addNewStudent tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item = createDataItem(index, ele);
                    $list.append($item);
                });
            },
            error: function () {
                alert("查询失败");
            }
        });
    });
    //4 体检批次界面操作栏 按钮的事件委托
    //4.1 显示信息表格编辑按钮监听事件
    $("#tb_addNewStudent").delegate(".addNewStudentInformationShowUpdateBtn", "click", function () {
        //获取当前批次记录的 id
        var id = $(this).attr("data-id");
        //4.1.1  获取修改前的数据
        var $value = $(this).parents("tr")[0];
        var num1 = $value.children[1].innerHTML;
        var num2 = $value.children[2].innerHTML;
        var num3 = $value.children[3].innerHTML;
        var num4 = $value.children[4].innerHTML;
        var num5 = $value.children[5].innerHTML;
        $("#addNewStudentUpdateBatchName").val(num1);
        $("#addNewStudentUpdateDepartmentName").val(num2);
        $("#addNewStudentUpdateBatchBeignTime").val(num3);
        $("#addNewStudentUpdateBatchEndTime").val(num4);
        $("#addNewStudentUpdateProjectName").val(num5);
        //显示编辑界面
        $("#over").css("display","block");
        $(".addNewStudentInformationShowUpdate").css("display", "block");
        ///4.1.2 编辑框确认按钮监听事件
        $("#addNewStudentInformationShowUpdateSureBtn").unbind("click").click(function () {
            //获取修改后的数据
            var batchname = $("#addNewStudentUpdateBatchName").val();
            //发送数据 修改信息
            $.ajax({
                url: "/hospital/phyStudent/update.action",
                type: "POST",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    "phyExmBatchName": batchname,
                    "id":id
                }),
                success: function (data) {
                    //编辑成功，返回成功数据
                    var $list = $("#tb_addNewStudent");
                    //1.清空表格数据
                    $("#tb_addNewStudent tr:not(:first)").html("");
                    //2.动态创建数据
                    $.each(data, function (index, ele) {
                        var $item = createDataItem(index, ele);
                        $list.append($item);
                    });
                    alert("修改成功");
                    //隐藏编辑界面
                    $("#over").css("display","none");
                    $(".addNewStudentInformationShowUpdate").css("display", "none");
                },
                error: function () {
                    alert("编辑失败");
                }
            });
        });
    });
    //4.1.3 编辑框返回按钮监听事件
    $("#addNewStudentInformationShowUpdateBackBtn").click(function () {
        $("#over").css("display","none");
        $(".addNewStudentInformationShowUpdate").css("display", "none");
    });
    // //4.2 显示信息表格删除按钮监听事件
    $("#tb_addNewStudent").delegate(".addNewStudentInformationShowCancelBtn", "click", function () {
        //获取当前体检批次 id
        var id = $(this).attr("data-id");
        // alert(id);
        $.ajax({
            url: "/hospital/phyStudent/delete.action",
            type: "POST",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({"id": id}),
            success: function (data) {
                //删除成功，返回成功数据
                var $list = $("#tb_addNewStudent");
                //1.清空表格数据
                $("#tb_addNewStudent tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item = createDataItem(index, ele);
                    $list.append($item);
                });
                alert("删除成功");
            },
            error: function () {
                alert("删除失败");
            }
        });
    });
});